JDBC - Lưu Vĩnh Tường - 22133064

Tạo thư mục JDBC

  • Tạo thư mục jdbc để chứa các chương trình và các file sau khi thông dịch JDBC Program
    Pasted image 20250326155000.png

Kiểm tra port

Pasted image 20250326160125.png

II. Config lại các file

core-site.xml

Pasted image 20250327130218.png

hdfs-site.xml

Pasted image 20250326163512.png

hive-site.xml

Pasted image 20250327124425.png

khởi chạy lại các dịch vụ

Pasted image 20250327124610.png

III. Khởi chạy các dịch vụ của hive

hive --service hiveserver2 &

Pasted image 20250327123710.png

kiểm tra đã chạy chưa, port đã mở chưa

netstat -tulnp | grep 10000

Pasted image 20250327131024.png
như hình là đã ổn

IV. Chương trình java

Code

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;


public class HiveCreateDb {
   private static String driverName = "org.apache.hive.jdbc.HiveDriver";
  
   public static void main(String[] args) throws SQLException, ClassNotFoundException {
      Class.forName(driverName);

     Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
      Statement stmt = con.createStatement();

      stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS userdb");
      System.out.println("Database 'userdb' created successfully.");
      
      stmt.close();
      con.close();
   }
}

Pasted image 20250327130032.png

Đóng gói và biên dịch

javac -cp "$(find "${HIVE_HOME}/lib" -name '*.jar' | tr 'n' ':')" HiveCreateDb.java

Pasted image 20250327123746.png

khởi chạy chương trình

java -cp "$(find "$HIVE_HOME/lib" -name '*.jar' | tr '\n' ':'):$(find "$HADOOP_HOME/share/hadoop/common/lib" -name '*.jar' | tr '\n' ':'):$(find "$HADOOP_HOME/share/hadoop/common" -name '*.jar' | tr '\n' ':'):./" HiveCreateDb

Pasted image 20250327125708.png

=> Kết quả trả về là tạo thành công => chương trình đã hoạt động

V. Demo

DropDB

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;


public class HiveDropDB {
   private static String driverName = "org.apache.hive.jdbc.HiveDriver";

   public static void main(String[] args) throws SQLException, ClassNotFoundException {
      Class.forName(driverName);

     Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");

      Statement stmt = con.createStatement();

      stmt.executeUpdate("DROP DATABASE userdb");
      System.out.println("Drop userdb database successful.");

      stmt.close();
      con.close();
   }
}

Pasted image 20250327153058.png

Create Table

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveCreateTable {
   private static String driverName = "org.apache.hive.jdbc.HiveDriver";
   public static void main(String[] args) throws SQLException, ClassNotFoundException {

      Class.forName(driverName);

      Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/userdb", "", "");

      Statement stmt = con.createStatement();

      stmt.executeUpdate("CREATE TABLE IF NOT EXISTS "
         + " employee (eid INT, name STRING, "
         + " salary STRING, destignation STRING) "
         + " COMMENT 'Employee details' "
         + " ROW FORMAT DELIMITED "
         + " FIELDS TERMINATED BY '\\t' "
         + " LINES TERMINATED BY '\\n' "
         + " STORED AS TEXTFILE");

      System.out.println("Table employee created.");

      con.close();
   }
}

Pasted image 20250327153613.png

Alter Table

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveAlterTable {
   private static String driverName = "org.apache.hive.jdbc.HiveDriver";
   public static void main(String[] args) throws SQLException, ClassNotFoundException {

      Class.forName(driverName);

      Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/userdb", "", "");
      Statement stmt = con.createStatement();
      stmt.executeUpdate("ALTER TABLE employee RENAME TO emp");
      System.out.println("Table Renamed Successfully");

      con.close();
   }
}

Pasted image 20250327154116.png

Drop table

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;


public class HiveDropTable {

   private static String driverName = "org.apache.hive.jdbc.HiveDriver";
   public static void main(String[] args) throws SQLException, ClassNotFoundException {

      Class.forName(driverName);

      Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/userdb", "", "");

      Statement stmt = con.createStatement();

      stmt.executeUpdate("DROP TABLE IF EXISTS employee");

      System.out.println("Drop table successful.");
      con.close();
   }
}

Pasted image 20250327154540.png